package com.example.demospringmybatisplus.handler;

import java.util.List;

import org.springframework.util.CollectionUtils;

/**
 * 数据校验责任链工具类
 *
 * @author huahua
 * @DATE 2024/8/9
 **/
public class ValidateChainUtil {

    public static <T> void doValidateChain(List<ValidateHandler<T>> chainNodeList, T validateData) {
        if (CollectionUtils.isEmpty(chainNodeList)) {
            return;
        }
        // 初始化调用链所有节点
        for (int i = 0; i < chainNodeList.size()-1; i++) {
            chainNodeList.get(i).setNext(chainNodeList.get(i + 1));
        }
        // 取第一个节点进行校验
        chainNodeList.get(0).doValidate(validateData);
    }
}
